/**
 * 起重机械配置（添加|修改）projectOption.js
 * 版本号： v1.0.0
 */
$(function(){
//所属公司下拉树
    $.ajax({
        url: CONTEXT_PATH + "/company/getTreeCompany.do",
        type: "get",
        success: function (data) {
            var isAdd = $("#cpId").val();
            var themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
            themecombo += data.substring(1);
            themecombo = JSON.parse(themecombo);
            $("#companyTree").data("companyList", themecombo);
            $("#companyTree").combotree("loadData",themecombo);
            $("#companyTree").combotree({
                onChange:function(node){
                    $('#companyId').val(node);
                    //在选择后根据选择公司的ID去查询部门下拉框
                    var companyId = node;
                    $("#officeTree").combotree("clear");
                    $.ajax({
                        url: CONTEXT_PATH + "/office/getTree.do",
                        data:{
                            "companyId":companyId
                        },
                        type: "get",
                        success: function (data) {
                            var themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
                            themecombo += data.substring(1);
                            themecombo = JSON.parse(themecombo);
                            $("#officeTree").data("officeList",themecombo);
                            $("#officeTree").combotree("loadData",themecombo);
                            $("#officeTree").combotree({
                                onChange:function(node){
                                    $('#officeId').val(node);
                                    var officeId = $("#officeId").val();
                                    companyId = $('#companyId').val();
                                    //在选择后根据选择公司的ID去查询项目下拉框
                                    $("#projectList").combobox("clear");
                                    $.ajax({
                                        url: CONTEXT_PATH + "/project/projectList.do",
                                        data:{
                                            "companyId":companyId,
                                            "officeId":officeId
                                        },
                                        type: "get",
                                        success: function (data) {
                                            var themecombo = "";
                                            if(data.length > 2){
                                                themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
                                                themecombo += data.substring(1);
                                            }else{
                                                themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"}]";
                                            }
                                            themecombo = JSON.parse(themecombo);
                                            $("#projectList").combobox("loadData",themecombo);
                                            $("#projectList").combobox({
                                                valueField:'id',
                                                textField:'text',
                                                onChange:function(node){
                                                    $('#prId_').val(node);
                                                }
                                            });
                                            if(!isAdd || isAdd == ""){
                                                $("#projectList").combobox("setValue","");
                                            }else{
                                                var projectId = $("#prId_").val();
                                                var oldProjectId = $("#oldPrId").val();
                                                var officeId = $("#officeId").val();
                                                var oldOfficeId = $("#oldOfficeId").val();
                                                var companyId = $("#companyId").val();
                                                var oldCompanyId = $("#oldCompanyId").val();
                                                if(oldProjectId == projectId && oldOfficeId == officeId && oldCompanyId == companyId){
                                                    $("#projectList").combobox("setValue",projectId);
                                                }else{
                                                    $("#projectList").combobox("setValue","");
                                                }
                                            }
                                        }
                                    });
                                }
                            });
                            if(!isAdd || isAdd == ""){
                                $("#officeTree").combotree("setValue","");
                            }else{
                                var officeId = $("#officeId").val();
                                var oldOfficeId = $("#oldOfficeId").val();
                                var companyId = $("#companyId").val();
                                var oldCompanyId = $("#oldCompanyId").val();
                                if(oldOfficeId == officeId && oldCompanyId == companyId){
                                    $("#officeTree").combotree("setValue",officeId);
                                }
                            }
                        }
                    });

                    //在选择后根据选择公司的ID去查询项目下拉框
                    $("#projectList").combobox("clear");
                    companyId = $('#companyId').val();
                    $.ajax({
                        url: CONTEXT_PATH + "/project/projectList.do",
                        data:{
                            "companyId":companyId
                        },
                        type: "get",
                        success: function (data) {
                            var themecombo = "";
                            if(data.length > 2){
                                themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
                                themecombo += data.substring(1);
                            }else{
                                themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"}]";
                            }
                            themecombo = JSON.parse(themecombo);
                            $("#projectList").combobox("loadData",themecombo);
                            $("#projectList").combobox({
                                valueField:'id',
                                textField:'text',
                                onChange:function(node){
                                    $('#prId_').val(node);
                                }
                            });
                            if(!isAdd || isAdd == ""){
                                $("#projectList").combobox("setValue","");
                            }else{
                                var projectId = $("#prId_").val();
                                var oldProjectId = $("#oldPrId").val();
                                var officeId = $("#officeId").val();
                                var oldOfficeId = $("#oldOfficeId").val();
                                var companyId = $("#companyId").val();
                                var oldCompanyId = $("#oldCompanyId").val();
                                if(oldProjectId == projectId && oldOfficeId == officeId && oldCompanyId == companyId){
                                    $("#projectList").combobox("setValue",projectId);
                                }else{
                                    $("#projectList").combobox("setValue","");
                                }
                            }
                        }
                    });
                }
            });
            $("#officeTree").combotree("setText","--请选择--");
            $("#projectList").combobox("setText","--请选择--");

            //修改操作时，数据加载完成后给各个选框赋值
            setValues(isAdd);
            setCraneStandardValue();
        }
    });

//设置输入框的最大限制长度
    $(".easyui-textbox").each(function(){
        var data_options = $(this).attr("data-options");
        if(data_options){
            var matchs = data_options.match(/length\[1,(\d*)\]/);
            if(matchs){
                $(this).textbox("textbox").attr("maxlength", matchs[1] - 0 + 1);
            }else{
                $(this).textbox("textbox").attr("maxlength", 20);
            }
        }else{
            $(this).textbox("textbox").attr("maxlength", 20);
        }
    });

    /**
     * 时间控制方法
     * @param date
     */
    $("#manufactureDate").datebox({
        onSelect : function(beginDate){
            $('#purchaseDate').datebox().datebox('calendar').calendar({
                validator: function(date){
                    return beginDate<=date;
                }
            });
        }
    });

    //加载起重机械所属的起重机械标准配置下拉框
    bindCombo_craneProjectSel("csId");

    /**
     * 保存按钮
     */
    $("#optionCraneProject").bind('click', function(){
        doOptionCraneProjectMethod();

    })
});


/**
 * 执行保存操作方法
 */
function doOptionCraneProjectMethod(){
    var params = $("#operate_form").serialize();
    $("#operate_form").form("submit",{
        cache:false,
        data: params,
        url: CONTEXT_PATH + "/craneProject/save.do",
        onSubmit:function() {
            preventSubmit('operate_form','optionCraneProject');
            return $(this).form("validate");
        },
        success:function(data) {
            var obj = JSON.parse(data);
            var url = null;
            if(source == "deviceManage"){
                url = CONTEXT_PATH + "/device/index.do";
            }else{
                url = CONTEXT_PATH + "/craneProject/index.do";
            }
            saveJump(obj,url);

        }
    });
}

/**
 * 验证只有英文和数字的方法
 */
$.extend($.fn.validatebox.defaults.rules, {
    englishCheckSub : { // 验证英文字母、数字
        validator : function(value) {
            return /^[a-zA-Z0-9]+$/.test(value);},
        message : "只能包括英文字母、数字"
    }
});

/**
 * 在修改页面的时候，获取对应的起重机械标准配置型号值，并给下拉框设置当前选中
 */
function setCraneStandardValue(){

    var csId  = $("#csId").val();
    if(csId != null&& csId != "" && csId != "0"){
        $("#csId").combobox('setValues',csId);
    }
}

/**
 * 在修改页面的时候，获取所属公司，部门，项目的值，并给下拉框设置当前选中
 */
function setValues(isAdd){
    //获取所属公司值，并给下拉框设置当前选中
    var companyId =$('#companyId').val();
    //获取所属部门值，并给下拉框设置当前选中
    var officeId = $("#officeId").val();
    var projectId = $('#prId_').val();
    if(companyId != null && companyId != "" && companyId != "0"){
        $('#companyTree').combotree('setValue', companyId);
    }


    if(officeId){
        $.ajax({
            url: CONTEXT_PATH + "/office/getTree.do",
            data:{
                "companyId":companyId
            },
            type: "get",
            success: function (data) {
                var themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
                themecombo += data.substring(1);
                themecombo = JSON.parse(themecombo);
                $("#officeTree").combotree("loadData",themecombo);
                $("#officeTree").combotree({
                    onChange:function(node){
                        $('#officeId').val(node);
                        var officeId = node;
                        companyId = $('#companyId').val();
                        //在选择后根据选择公司的ID去查询项目下拉框
                        $("#projectList").combobox("clear");
                        $.ajax({
                            url: CONTEXT_PATH + "/project/projectList.do",
                            data:{
                                "companyId":companyId,
                                "officeId":officeId
                            },
                            type: "get",
                            success: function (data) {
                                var themecombo = "";
                                if(data.length > 2){
                                    themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
                                    themecombo += data.substring(1);
                                }else{
                                    themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"}]";
                                }
                                themecombo = JSON.parse(themecombo);
                                $("#projectList").combobox("loadData",themecombo);
                                $("#projectList").combobox({
                                    valueField:'id',
                                    textField:'text',
                                    onChange:function(node){
                                        $('#prId_').val(node);
                                    }
                                });
                                if(!isAdd || isAdd == ""){
                                    $("#projectList").combobox("setValue","");
                                }else{
                                    var projectId = $("#prId_").val();
                                    var oldProjectId = $("#oldPrId").val();
                                    var officeId = $("#officeId").val();
                                    var oldOfficeId = $("#oldOfficeId").val();
                                    var companyId = $("#companyId").val();
                                    var oldCompanyId = $("#oldCompanyId").val();
                                    if(oldProjectId == projectId && oldOfficeId == officeId && oldCompanyId == companyId){
                                        $("#projectList").combobox("setValue",projectId);
                                    }
                                }
                            }
                        });
                    }
                });
                $("#officeTree").combotree("setValue",officeId);

                //获取所属项目的值，并给下拉框设置选中
                if(companyId && projectId){

                    //在选择后根据选择公司的ID去查询项目下拉框
                    $.ajax({
                        url: CONTEXT_PATH + "/project/projectList.do",
                        data:{
                            "companyId":companyId,
                            "officeId":officeId
                        },
                        type: "get",
                        success: function (data) {
                            var themecombo = "";
                            if(data.length > 2){
                                themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
                                themecombo += data.substring(1);
                            }else{
                                themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"}]";
                            }
                            themecombo = JSON.parse(themecombo);
                            $("#projectList").combobox("loadData",themecombo);
                            $("#projectList").combobox({
                                valueField:'id',
                                textField:'text',
                                onChange:function(node){
                                    $('#prId_').val(node);
                                }
                            });
                            $("#projectList").combobox("setValue",projectId);
                        }
                    });
                }
            }
        });
    }else{
        //获取所属项目的值，并给下拉框设置选中
          if(companyId && projectId){
            //在选择后根据选择公司的ID去查询项目下拉框
            $.ajax({
                url: CONTEXT_PATH + "/project/projectList.do",
                data:{
                    "companyId":companyId
                },
                type: "get",
                success: function (data) {
                    var themecombo = "";
                    if(data.length > 2){
                        themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"},";
                        themecombo += data.substring(1);
                    }else{
                        themecombo = "[{\"text\": \"--请选择--\", \"id\": \"\"}]";
                    }
                    themecombo = JSON.parse(themecombo);
                    $("#projectList").combobox("loadData",themecombo);
                    $("#projectList").combobox({
                        valueField:'id',
                        textField:'text',
                        onChange:function(node){
                            $('#prId_').val(node);
                        }
                    });
                    $("#projectList").combobox("setValue",projectId);
                }
            });
        }
    }
}

/**
 * 所属起重机械标准配置bind事件
 * @param deviceControl
 */
function bindCombo_craneProjectSel(craneProjectControl){
    $.ajax({
        url: CONTEXT_PATH + "/craneStandard/getCraneStandardList.do",
        type: "get",
        success: function (data) {
            var obj = JSON.parse(data);
            var craneProjectCombo = [{'text': '--请选择--','value': ''}];
            for (var i = 0; i < obj.length; i++) {
                craneProjectCombo.push({"text": obj[i].text, "value": obj[i].id});
            }
            $("#" + craneProjectControl).combobox("loadData", craneProjectCombo);
            $("#" + craneProjectControl).combobox({
                required:true,
                onSelect: function(rec){
                    $('#csId_').val(rec.value);
                }
            });
            var csId = $('#csId_').val();
            if(csId){
                $("#" + craneProjectControl).combobox("setValue", csId);
            }else{
                $("#" + craneProjectControl).combobox("setValue", '');
            }

        }
    })
}

/**
 * 时间控制方法
 * @param date
 */
function onSelect(date){  //开始日期选择时触发
    $('#manufactureDate').datebox('enable');	//启用结束日期控件
    $('#purchaseDate').datebox('reset')		//重置结束日期的值
};
